package com.zmcn.flowindex.business.title.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zmcn.flowindex.bean.po.IndexInfo;
import com.zmcn.flowindex.bean.po.IndexTitle;
import com.zmcn.flowindex.bean.po.UploadBatch;
import com.zmcn.flowindex.bean.vo.IndexTitleVO;
import com.zmcn.flowindex.business.title.dao.titleDao;
import com.zmcn.flowindex.business.title.service.titleService;
import com.zmcn.flowindex.business.title.vo.IndexInfoVO;
import com.zmcn.flowindex.business.title.vo.ShowVO;
import com.zmcn.flowindex.fw.action.Page;
import com.zmcn.flowindex.fw.util.FileOP;
import com.zmcn.flowindex.fw.util.ParseUtil;

/**
 * 
 * 指标主题业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class titleServiceImpl implements titleService {
	private titleDao dao;

	@Autowired
	public void setDao(titleDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取指标主题列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public Page<ShowVO> getList(Page<ShowVO> page) {
		return dao.getList(page);
	}

	/**
	 * 导入数据
	 * 
	 * @param note
	 *            上传描述
	 * @param fileName
	 *            文件名
	 * @param file
	 *            文件流
	 * @param user
	 *            当前用户
	 */
	@Transactional(readOnly = false)
	public void imp(String note, String[] fileName, File[] file, String user) {
		Date date = new Date();
		UploadBatch ub = new UploadBatch();
		ub.setNote(note);
		ub.setCreator(user);
		ub.setCreateTime(date);
		ub.setLastEditor(user);
		ub.setLastUpdateTime(date);
		ub.setStatus(0);
		dao.create(ub);

		// 解析文件内容
		for (int i = 0; i < fileName.length; i++) {
			if (null != fileName && !"".equals(fileName)) {
				doOneFile(ub.getBatchId(), fileName[i], file[i]);
			}
		}
	}

	// 解析一个主题指标数
	private void doOneFile(String batchId, String fileName, File file) {
		String titleName = fileName.substring(0, fileName.lastIndexOf("."));
		//
		IndexTitle it = new IndexTitle();
		it.setBatchId(batchId);
		it.setTitleName(titleName);
		dao.create(it);

		// 解析文件内容
		String c = FileOP.readFile(file, "gb2312");
		String[] line = c.split("\r\n");
		IndexInfo[] temp = new IndexInfo[10];
		for (int i = 0; i < line.length; i++) {
			String[] s = ParseUtil.splitByDot(line[i]);
			// 构建数据
			IndexInfo info = new IndexInfo();
			info.setTitleId(it.getTitleId());
			int p = 0;
			for (int j = 0; j < s.length; j++) {
				if (null != s[j] && !"".equals(s[j])) {
					if (0 == p) {
						info.setIndexName(s[j]);
						if (0 == j) {
							info.setPid("");
						} else {
							info.setPid(temp[j - 1].getIndexId());
						}
						temp[j] = info;
					} else {
						info.setIndexValue(s[j]);
					}
					p++;
				}
			}
			// 保存数据
			dao.create(info);
		}
	}

	/**
	 * 获取某主题的节点树数据
	 * 
	 * @param titleId
	 *            主题ID
	 * @return
	 */
	public JSONArray getTreeByTitle(String titleId) {
		List<IndexInfoVO> list = dao.getTreeByTitle(titleId);
		return getTreeDataByPid(list, "");
	}

	/**
	 * 获取一个节点下所有子节点形成的JSONArray
	 * 
	 * @param sou
	 * @param parentId
	 * @return
	 */
	private static JSONArray getTreeDataByPid(List<IndexInfoVO> sou, String id) {
		JSONArray result = new JSONArray();
		for (IndexInfoVO q : sou) {
			if (id.equals(q.getPid())) {
				JSONObject jobj = new JSONObject();
				jobj.put("id", q.getIndexId());
				jobj.put("text", q.getIndexName() + (null == q.getIndexValue() ? "" : "_" + q.getIndexValue()));
				JSONObject att = new JSONObject();
				att.put("pid", q.getPid());
				att.put("indexName", q.getIndexName());
				att.put("indexValue", q.getIndexValue());
				jobj.put("attributes", att);
				JSONArray tmp = getTreeDataByPid(sou, q.getIndexId());
				if (tmp.size() > 0) {
					jobj.put("state", "closed");
					jobj.put("children", tmp);
				}
				result.add(jobj);
			}
		}
		return result;
	}

	/**
	 * 删除指标主题
	 * 
	 * @param uploadTime
	 *            删除时间
	 * @param user
	 *            当前用户
	 * @return
	 */
	@Transactional(readOnly = false)
	public void remove(String uploadTime, String user) {
		dao.remove(uploadTime, user);
	}

	/**
	 * 获取最后上传指标的标题列表
	 * 
	 * @return
	 */
	public List<IndexTitleVO> getLastTitles() {
		return dao.getLastTitles();
	}

	/**
	 * 获取前台数数据
	 * 
	 * @param titleId
	 * @return
	 */
	public List<IndexInfoVO> getFrontTree(String titleId) {
		List<IndexInfoVO> list = dao.getTreeByTitle(titleId);
		return getFrontTreeDataByPid(list, "", 0);
	}

	/**
	 * 获取一个节点下所有子节点形成的JSONArray
	 * 
	 * @param sou
	 * @param parentId
	 * @return
	 */
	private static List<IndexInfoVO> getFrontTreeDataByPid(List<IndexInfoVO> sou, String id, int level) {
		List<IndexInfoVO> result = new ArrayList<IndexInfoVO>();
		for (IndexInfoVO q : sou) {
			if (id.equals(q.getPid())) {
				q.setLevel(level);
				result.add(q);
				result.addAll(getFrontTreeDataByPid(sou, q.getIndexId(), level + 1));
			}
		}
		return result;
	}

}
